package com.wxh.multithread;

/**
 * @ClassName: TestDaemon
 * @Description: 测试守护线程
 * @Author wxh
 * @Date: 2022/1/6 10:42
 * @Version V1.0.0
 * @Since 1.8
 */
public class TestDaemon {
    /**
     * 线程分用户线程(main)和守护线程(gc)--后台记录日志、监控内存、垃圾回收等
     * @param args
     */
    public static void main(String[] args) {
        God god = new God();
        You2 you2 = new You2();

        Thread thread = new Thread(god);
        // false表示用户线程, true表示守护线程
        thread.setDaemon(true);
        // 启动守护线程
        thread.start();

        // 启动用户线程
        new Thread(you2).start();
    }
}

class God implements Runnable{
    @Override
    public void run() {
        while (true) {
            System.out.println("上帝保佑你");
        }
    }
}

class You2 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 36500; i++) {
            System.out.println("你一生都开心的活着");
        }
        System.out.println("==============用户线程结束===============");
    }
}